package org.edu.modules.base.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.base.entity.*;
import org.edu.modules.base.service.IVeBaseSysMenuService;
import org.edu.modules.base.service.IVeBaseSysRoleMenuService;
import org.edu.modules.base.service.IVeBaseSysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = {"角色管理"})
@RestController
@RequestMapping("/base/VeBaseSysRole")
@ApiSort(5)
public class VeBaseSysRoleController {

    @Autowired
    private IVeBaseSysRoleService veBaseSysRoleService;

    @Autowired
    private IVeBaseSysRoleMenuService veBaseSysRoleMenuService;

    @AutoLog("角色管理-分页列表查询")
    @ApiOperation(value = "角色管理-分页列表查询", notes = "角色管理-分页列表查询")
    @GetMapping({"/getSysRolePageList"})
    public Result<?> getSysRolePageList(VeBaseSysRole veBaseSysRole, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        int num = this.veBaseSysRoleService.getSysRoleAllList(veBaseSysRole);
        PageBean pb = new PageBean(pageNo.intValue(), pageSize.intValue(), num);
        veBaseSysRole.setStartIndex(Integer.valueOf(pb.getStartIndex()));
        veBaseSysRole.setPageSize(pageSize);
        pb.setList(this.veBaseSysRoleService.getSysRolePageList(veBaseSysRole));
        return Result.ok(pb);
    }

    @AutoLog("角色管理-添加")
    @ApiOperation(value = "角色管理-添加", notes = "角色管理-添加")
    @PostMapping({"/addSysRoleAndMenu"})
    public Result<?> addSysRoleAndMenu(@RequestBody VeBaseSysRole veBaseSysRole) {
        if (veBaseSysRole == null) {
            return Result.error("角色不能为空!");
        }
        VeBaseSysRole model = this.veBaseSysRoleService.getModelByRoleText(veBaseSysRole.getText());
        if (model != null) {
            return Result.error("角色号已存在!");
        }
        this.veBaseSysRoleService.addSysRole(veBaseSysRole);
        VeBaseSysRole model1 = this.veBaseSysRoleService.getModelByRoleText(veBaseSysRole.getText());
        if (veBaseSysRole.getIds() != null) {//lbh 20240618 新增为null判断
            if (veBaseSysRole.getIds().length > 0) {
                List<VeBaseSysRoleMenu> detailList = new ArrayList();
                for (String menuId : veBaseSysRole.getIds()) {
                    VeBaseSysRoleMenu veBaseSysRoleMenu = new VeBaseSysRoleMenu();
                    veBaseSysRoleMenu.setRoleId(model1.getId());
                    veBaseSysRoleMenu.setMenuId(Integer.valueOf(Integer.parseInt(menuId)));
                    veBaseSysRoleMenu.setAppId(veBaseSysRole.getAppId());
                    detailList.add(veBaseSysRoleMenu);
                }
                this.veBaseSysRoleMenuService.saveBatch(detailList);
            }
        }
        return Result.ok("添加成功!");
    }

    @AutoLog("角色管理-修改")
    @ApiOperation(value = "角色管理-修改", notes = "角色管理-修改")
    @PostMapping({"/editSysRoleAndMenu"})
    public Result<?> editSysRoleAndMenu(@RequestBody VeBaseSysRole veBaseSysRole) {
        if (veBaseSysRole == null) {
            return Result.error("角色不能为空!");
        }
        VeBaseSysRole model = this.veBaseSysRoleService.getModelByIdAndText(veBaseSysRole.getId(), veBaseSysRole.getText());
        if (model != null) {
            return Result.error("角色号已存在!");
        }
        this.veBaseSysRoleService.updateById(veBaseSysRole);
        this.veBaseSysRoleMenuService.deleteModelByAppId(veBaseSysRole.getId(), veBaseSysRole.getAppId());
        if (veBaseSysRole.getIds() != null) { //20240618 新增为null判断
            if (veBaseSysRole.getIds().length > 0) {
                List<VeBaseSysRoleMenu> detailList = new ArrayList();
                for (String menuId : veBaseSysRole.getIds()) {
                    VeBaseSysRoleMenu veBaseSysRoleMenu = new VeBaseSysRoleMenu();
                    veBaseSysRoleMenu.setRoleId(veBaseSysRole.getId());
                    veBaseSysRoleMenu.setMenuId(Integer.valueOf(Integer.parseInt(menuId)));
                    veBaseSysRoleMenu.setAppId(veBaseSysRole.getAppId());
                    detailList.add(veBaseSysRoleMenu);
                }
                this.veBaseSysRoleMenuService.saveBatch(detailList);
            }
        }

        return Result.ok("修改成功!");
    }

    @AutoLog("角色管理-通过id删除")
    @ApiOperation(value = "角色管理-通过id删除", notes = "角色管理-通过id删除")
    @PostMapping({"/deleteSysRole"})
    @Transactional
    public Result<?> deleteSysRole(@RequestParam(name = "id", required = true) String id) {
        if (id.equals("6")) {
            return Result.error("管理员不能删除! ");
        }
        if ((id.equals("37")) || (id.equals("38"))) {
            return Result.error("企业管理员或者企业教师不能删除! ");
        }
        this.veBaseSysRoleService.removeById(id);
        this.veBaseSysRoleMenuService.deleteRoleUserByRoleId(id);
        this.veBaseSysRoleMenuService.deleteRoleMenuByRoleId(id);
        return Result.ok("删除成功!");
    }
}
